﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CUiTcNewObjectForm
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;
using Tecnomatix.Engineering.Ui;
using UiInternalControls.WindowsFormsControls;


namespace DnProcessSimulateCommands;

public class CUiTcNewObjectForm : TxForm
{
  private const string OBJECT_DESCRIPTION_DIRECTION_PROP_NAME = "ObjectDescriptionDirection";
  private const string OBJECT_NAME_DIRECTION_PROP_NAME = "ObjectNameDirection";
  private const int GWL_EXSTYLE = -20;
  private const int WS_EX_RTLREADING = 8192 /*0x2000*/;
  private Label label1;
  private TxConstrainedEditControl m_objName;
  private Label label2;
  private Label label3;
  private Button m_closeBtn;
  private Button m_applyBtn;
  private Button m_okBtn;
  private Container components;
  private ITxObject m_parentObj;
  private TxListView m_typesListView;
  private TextBox m_objDescription;
  private ColumnHeader m_columnHeader;
  private CApTcNewObject m_app;

  public CUiTcNewObjectForm(ITxObject obj, CApTcNewObject apTcNewObj)
  {
    this.InitializeComponent();
    this.m_parentObj = obj;
    this.m_app = apTcNewObj;
    ((TextBoxBase) this.m_objName).MaxLength = (int) TxTcUtilities.TcObjectNameMaxLength();
    this.m_objName.ContentValidChanged += (EventHandler) ((sender, e) => this.EnableOkButton());
    ((TxLOVEditBoxControl) this.m_objName).FilterOutSearchResults = true;
  }

  protected virtual void Dispose(bool disposing)
  {
    if (disposing && this.components != null)
      this.components.Dispose();
    base.Dispose(disposing);
  }

  private void InitializeComponent()
  {
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CUiTcNewObjectForm));
    this.label1 = new Label();
    this.m_objName = new TxConstrainedEditControl();
    this.label2 = new Label();
    this.label3 = new Label();
    this.m_closeBtn = new Button();
    this.m_applyBtn = new Button();
    this.m_okBtn = new Button();
    this.m_typesListView = new TxListView();
    this.m_columnHeader = new ColumnHeader();
    this.m_objDescription = new TextBox();
    ((Control) this).SuspendLayout();
    componentResourceManager.ApplyResources((object) this.label1, "label1");
    this.label1.Name = "label1";
    componentResourceManager.ApplyResources((object) this.m_objName, "m_objName");
    ((Control) this.m_objName).Name = "m_objName";
    ((Control) this.m_objName).TextChanged += new EventHandler(this.OnNameChanged);
    componentResourceManager.ApplyResources((object) this.label2, "label2");
    this.label2.Name = "label2";
    componentResourceManager.ApplyResources((object) this.label3, "label3");
    this.label3.Name = "label3";
    componentResourceManager.ApplyResources((object) this.m_closeBtn, "m_closeBtn");
    this.m_closeBtn.DialogResult = DialogResult.Cancel;
    this.m_closeBtn.Name = "m_closeBtn";
    this.m_closeBtn.Click += new EventHandler(this.m_closeBtn_Click);
    componentResourceManager.ApplyResources((object) this.m_applyBtn, "m_applyBtn");
    this.m_applyBtn.Name = "m_applyBtn";
    this.m_applyBtn.Click += new EventHandler(this.m_applyBtn_Click);
    componentResourceManager.ApplyResources((object) this.m_okBtn, "m_okBtn");
    this.m_okBtn.Name = "m_okBtn";
    this.m_okBtn.Click += new EventHandler(this.m_okBtn_Click);
    componentResourceManager.ApplyResources((object) this.m_typesListView, "m_typesListView");
    ((ListView) this.m_typesListView).Columns.AddRange(new ColumnHeader[1]
    {
      this.m_columnHeader
    });
    ((ListView) this.m_typesListView).HideSelection = false;
    ((ListView) this.m_typesListView).MultiSelect = false;
    ((Control) this.m_typesListView).Name = "m_typesListView";
    ((ListView) this.m_typesListView).Sorting = SortOrder.Ascending;
    ((ListView) this.m_typesListView).UseCompatibleStateImageBehavior = false;
    ((ListView) this.m_typesListView).View = View.Details;
    ((ListView) this.m_typesListView).SelectedIndexChanged += new EventHandler(this.m_typesListView_SelectedIndexChanged);
    ((Control) this.m_typesListView).SizeChanged += new EventHandler(this.m_typesListView_SizeChanged);
    componentResourceManager.ApplyResources((object) this.m_columnHeader, "m_columnHeader");
    componentResourceManager.ApplyResources((object) this.m_objDescription, "m_objDescription");
    this.m_objDescription.Name = "m_objDescription";
    ((Form) this).AcceptButton = (IButtonControl) this.m_okBtn;
    componentResourceManager.ApplyResources((object) this, "$this");
    ((Form) this).CancelButton = (IButtonControl) this.m_closeBtn;
    ((Control) this).Controls.Add((Control) this.m_objDescription);
    ((Control) this).Controls.Add((Control) this.m_objName);
    ((Control) this).Controls.Add((Control) this.m_typesListView);
    ((Control) this).Controls.Add((Control) this.m_okBtn);
    ((Control) this).Controls.Add((Control) this.m_applyBtn);
    ((Control) this).Controls.Add((Control) this.m_closeBtn);
    ((Control) this).Controls.Add((Control) this.label3);
    ((Control) this).Controls.Add((Control) this.label2);
    ((Control) this).Controls.Add((Control) this.label1);
    ((Form) this).MaximizeBox = false;
    ((Form) this).MinimizeBox = false;
    ((Control) this).Name = nameof (CUiTcNewObjectForm);
    ((Form) this).SizeGripStyle = SizeGripStyle.Hide;
    ((Form) this).Load += new EventHandler(this.OnLoad);
    ((Control) this).ResumeLayout(false);
    ((Control) this).PerformLayout();
  }

  private string GetTypeNameFromSelectedItem()
  {
    string fromSelectedItem = "";
    if (((ListView) this.m_typesListView).SelectedItems.Count > 0)
      fromSelectedItem = (((ListView) this.m_typesListView).SelectedItems[0].Tag as TxPlanningTypeMetaData).TypeName;
    return fromSelectedItem;
  }

  private void m_closeBtn_Click(object sender, EventArgs e) => ((Form) this).Close();

  private void m_applyBtn_Click(object sender, EventArgs e)
  {
    if (!this.CreateObject())
      return;
    this.m_objDescription.Clear();
  }

  private void m_okBtn_Click(object sender, EventArgs e)
  {
    if (!this.CreateObject())
      return;
    ((Form) this).Close();
  }

  private bool CreateObject()
  {
    if (!this.m_objName.ContentValid)
    {
      int num = (int) MessageBox.Show(StringTable.NEW_OBJECT_CMD_NAME_OUTSIDE_LOV, StringTable.NEW_OBJECT_CMD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
      return false;
    }
    this.m_app.Create(((Control) this.m_objName).Text, this.m_objDescription.Text, this.GetTypeNameFromSelectedItem());
    return true;
  }

  private void OnLoad(object sender, EventArgs e)
  {
    this.RegisterToDocumentUnLoadEvent();
    this.m_okBtn.Enabled = false;
    this.m_applyBtn.Enabled = false;
    this.m_objDescription.Enabled = false;
    this.LoadTypes();
  }

  protected virtual void OnClosed(EventArgs e)
  {
    base.OnClosed(e);
    this.UnRegisterToDocumentUnloadEvent();
  }

  protected virtual bool ProcessCmdKey(ref Message msg, Keys keyData)
  {
    // ISSUE: explicit non-virtual call
    // ISSUE: explicit non-virtual call
    return __nonvirtual (((ContainerControl) this).ActiveControl) == this.m_objName && keyData == Keys.Return || __nonvirtual (((Form) this).ProcessCmdKey(ref msg, keyData));
  }

  private void RegisterToDocumentUnLoadEvent()
  {
    TxApplication.ActiveDocument.Unloading += new TxDocument_UnloadingEventHandler(this.OnUnloadDocument);
  }

  private void UnRegisterToDocumentUnloadEvent()
  {
    TxApplication.ActiveDocument.Unloading -= new TxDocument_UnloadingEventHandler(this.OnUnloadDocument);
  }

  private void OnUnloadDocument(object sender, TxDocument_UnloadingEventArgs args)
  {
    ((Form) this).Close();
  }

  private void OnNameChanged(object sender, EventArgs e) => this.EnableOkButton();

  private void m_typesListView_SelectedIndexChanged(object sender, EventArgs e)
  {
    TxPlanningTypeMetaData typeMetaData = CApTcNewObject.GetTypeMetaData(this.GetTypeNameFromSelectedItem());
    ((Control) this.m_objName).Text = string.Empty;
    this.m_objName.SetFieldMetadata(typeMetaData, "object_name");
    this.m_objDescription.Enabled = this.m_app.CanHaveDescription(typeMetaData);
    if (typeMetaData != null && this.IsObjectNameEmptyOrSelectedType(((Control) this.m_objName).Text.TrimEnd()))
    {
      if (!this.m_objName.IsLovPresent && ((ListView) this.m_typesListView).SelectedItems.Count > 0)
        ((Control) this.m_objName).Text = ((ListView) this.m_typesListView).SelectedItems[0].Text + "*";
      this.SetDescriptionFieldLimitation(typeMetaData.TypeName);
    }
    this.EnableOkButton();
  }

  private void m_typesListView_SizeChanged(object sender, EventArgs e)
  {
    TxListView typesListView = this.m_typesListView;
    ((ListView) typesListView).Columns[0].Width = ((Control) typesListView).ClientSize.Width;
  }

  private void EnableOkButton()
  {
    bool flag = ((Control) this.m_objName).Text.Trim().Length > 0 && ((ListView) this.m_typesListView).SelectedItems.Count > 0;
    this.m_okBtn.Enabled = flag;
    this.m_applyBtn.Enabled = flag;
  }

  private bool IsObjectNameEmptyOrSelectedType(string objectName)
  {
    return objectName == "" || ((ListView) this.m_typesListView).SelectedItems.Count > 0 && objectName == ((ListView) this.m_typesListView).SelectedItems[0].Text;
  }

  private void LoadTypes()
  {
    ArrayList typesToDisplay = this.m_app.GetTypesToDisplay();
    ((ListView) this.m_typesListView).Items.Clear();
    ((ListView) this.m_typesListView).SmallImageList = new ImageList();
    foreach (TxPlanningTypeMetaData planningTypeMetaData in typesToDisplay)
    {
      ListViewItem listViewItem = new ListViewItem(planningTypeMetaData.DisplayName);
      Image iconFromType = this.m_app.GetIconFromType(planningTypeMetaData.TypeName);
      if (iconFromType != null)
        listViewItem.ImageIndex = ((ListView) this.m_typesListView).SmallImageList.Images.Add(iconFromType, Color.White);
      listViewItem.Tag = (object) planningTypeMetaData;
      ((ListView) this.m_typesListView).Items.Add(listViewItem);
    }
  }

  private void SetDescriptionFieldLimitation(string typeName)
  {
    if (!this.m_objDescription.Enabled)
      return;
    int descriptionFieldLength = TxObjectEx.GetDescriptionFieldLength(typeName);
    if (descriptionFieldLength <= 0 || descriptionFieldLength == this.m_objDescription.MaxLength)
      return;
    this.m_objDescription.MaxLength = descriptionFieldLength;
  }
}
